package http

import (
	"engine/internal"
	"gitee.com/ameise84/e3dds"
	"github.com/gin-gonic/gin"
)

var (
	_gService     httpServer
	_gActorServer e3dds.NetService
)

func Start(opts internal.Options, netService e3dds.NetService, msgHand internal.NetMsgTransformHandler) (addr string, err error) {
	_gActorServer = netService

	if err = _gService.create(opts); err != nil {
		return "", err
	}
	defer func() {
		if err != nil {
			_gService.destroy()
		}
	}()
	return _gService.start()
}

func Stop() {
	_gService.stop()
}

func SetLogOKRsp(v bool) {
	_gLogOkRsp = v
}

func Use(middleware ...gin.HandlerFunc) {
	_gHttpEngine.Use(middleware...)
}

func Get(relativePath string, handlers ...gin.HandlerFunc) {
	hand := make([]gin.HandlerFunc, 0, len(handlers)+2)
	if len(handlers) > 0 {
		hand = append(hand, handlers...)
	}
	hand = append(hand, initReq, onReq)
	_gHttpEngine.GET(relativePath, hand...)
}

func Post(relativePath string, handlers ...gin.HandlerFunc) {
	hand := make([]gin.HandlerFunc, 0, len(handlers)+2)
	if len(handlers) > 0 {
		hand = append(hand, handlers...)
	}
	hand = append(hand, initReq, onReq)
	_gHttpEngine.POST(relativePath, hand...)
}

func Del(relativePath string, handlers ...gin.HandlerFunc) {
	hand := make([]gin.HandlerFunc, 0, len(handlers)+2)
	if len(handlers) > 0 {
		hand = append(hand, handlers...)
	}
	hand = append(hand, initReq, onReq)
	_gHttpEngine.DELETE(relativePath, hand...)
}

func Any(relativePath string, handlers ...gin.HandlerFunc) {
	hand := make([]gin.HandlerFunc, 0, len(handlers)+2)
	if len(handlers) > 0 {
		hand = append(hand, handlers...)
	}
	hand = append(hand, initReq, onReq)
	_gHttpEngine.Any(relativePath, hand...)
}

func Match(methods []string, relativePath string, handlers ...gin.HandlerFunc) {
	hand := make([]gin.HandlerFunc, 0, len(handlers)+2)
	if len(handlers) > 0 {
		hand = append(hand, handlers...)
	}
	hand = append(hand, initReq, onReq)
	_gHttpEngine.Match(methods, relativePath, hand...)
}
